package com.qinggeng.project.system.controller;

import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.utils.SecurityUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.GoodsGift;
import com.qinggeng.project.system.domain.json.GoodsGiftId;
import com.qinggeng.project.system.service.IGiftProbabilityService;
import com.qinggeng.project.system.service.IGoodsGiftService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.List;

/**
 * 商品赠品Controller
 *
 * @author qinggeng
 * @date 2020-07-16
 */
@RestController
@RequestMapping("/system/gift")
public class GoodsGiftController extends BaseController {
    @Autowired
    private IGoodsGiftService goodsGiftService;
    @Autowired
    private IGiftProbabilityService probabilityService;

    /**
     * 查询商品赠品列表
     */
    @PreAuthorize("@ss.hasPermi('system:gift:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsGift goodsGift) {
        Long shopId = SecurityUtils.getShopId();
//        if (shopId == null) {
//            goodsGift.setShopId(0l);
//        } else {
//            goodsGift.setShopId(shopId);
//        }
        if (shopId != null){
            goodsGift.setShopId(shopId);
        }
        startPage();
        List<GoodsGift> list = goodsGiftService.selectGoodsGiftList(goodsGift);
        return getDataTable(list);
    }

    /**
     * 导出商品赠品列表
     */
    @PreAuthorize("@ss.hasPermi('system:gift:export')")
    @Log(title = "商品赠品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GoodsGift goodsGift) {
        List<GoodsGift> list = goodsGiftService.selectGoodsGiftList(goodsGift);
        ExcelUtil<GoodsGift> util = new ExcelUtil<GoodsGift>(GoodsGift.class);
        return util.exportExcel(list, "gift");
    }

    /**
     * 获取商品赠品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:gift:query')")
    @GetMapping(value = "/{giftId}")
    public AjaxResult getInfo(@PathVariable("giftId") Long giftId) {
        return AjaxResult.success(goodsGiftService.selectGoodsGiftById(giftId));
    }

    /**
     * 新增奖品
     */
    @PreAuthorize("@ss.hasPermi('system:gift:add')")
    @Log(title = "奖品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsGift goodsGift) {
        //设置奖品中奖奖
        goodsGift.setProbability(probabilityService.getGiftProbability(goodsGift.getGiftRate()));
        Long shopId = SecurityUtils.getShopId();
        if (shopId == null) {
            goodsGift.setShopId(0L);
        } else {
            goodsGift.setShopId(shopId);
        }
        return toAjax(goodsGiftService.insertGoodsGift(goodsGift));
    }

    /**
     * 修改商品赠品
     */
    @PreAuthorize("@ss.hasPermi('system:gift:edit')")
    @Log(title = "商品赠品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsGift goodsGift) {
        //设置奖品中奖奖
        goodsGift.setProbability(probabilityService.getGiftProbability(goodsGift.getGiftRate()));
        return toAjax(goodsGiftService.updateGoodsGift(goodsGift));
    }

    /**
     * 删除商品赠品
     */
    @PreAuthorize("@ss.hasPermi('system:gift:remove')")
    @Log(title = "商品赠品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{giftIds}")
    public AjaxResult remove(@PathVariable Long[] giftIds) {
        GoodsGift where=new GoodsGift();
        where.setGiftType(CommonEnum.STATUS_20.getValue());
        List<GoodsGift> goodsGiftList=goodsGiftService.selectGoodsGiftList(where);
        for (GoodsGift goodsGift : goodsGiftList) {
            if(ArrayUtils.contains(giftIds,goodsGift.getGiftId())){
                return AjaxResult.error(goodsGift.getGiftName()+"为系统参数，不允许删除!");
            }
        }
        return toAjax(goodsGiftService.deleteGoodsGiftByIds(giftIds));
    }
}
